#include <stdio.h>
#include "gateway_client.h"
#include "gateway_acceptor.hpp"
#include "gateway_taskpool.h"
#include "../../liblog/Include/liblog.h"
#include "../pagefile/pagefile.h"
#include "gateway_cache.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <signal.h>
#include <unistd.h>

class gateway_acceptor_observer : public gateway_acceptor_event{
public:
	virtual int acceptor_notify(gateway_stream_ptr stream_ptr){
		stream_ptr->set_event_handle(new gateway_client(stream_ptr));
		return 0;
	}
};

void run()
{
	static bool is_run = true;
#if defined(WIN32)
	LogInfo(("Press 'Q' or 'q' to quit!"));
#elif defined(__linux__)
	struct _SIGNAL
	{
		static void OnCaptureSignal(int sigl)
		{
			printf("SIGNAL %d Captured.\r\n", sigl);
			LibLog_FlushFile();
		}

		static void OnExitProcess(int sigl)
		{
			fflush(NULL);
			is_run = false;
		}
	};

	signal(SIGPIPE, _SIGNAL::OnCaptureSignal);
	signal(SIGUSR1, _SIGNAL::OnCaptureSignal);
	signal(SIGUSR2, _SIGNAL::OnCaptureSignal);
	signal(SIGTSTP, _SIGNAL::OnExitProcess); // Ctrl + Z
	signal(SIGINT, _SIGNAL::OnExitProcess);  // Ctrl + C
	LogInfo(("Press 'Ctrl + Z' or 'Ctrl + C' to quit!"));
#endif

	do
	{
#if defined(WIN32)
		char c = getch();
		if (c == 'q' || c == 'Q')
			is_run = false;
		else if(c == 'f' || c == 'F')
			LibLog_FlushFile();
#else
		//getchar();
		sleep(5);
#endif
	} while (is_run);
}

std::string GetCompileVersion()
{
#ifdef NDEBUG
	const char* configType = "Release";
#else
	const char* configType = "Debug";
#endif

	char buff[50] = { 0 };
	unsigned int nYear, nMonth, nDay;
	sscanf(__DATE__, "%36s %d %d", buff, &nDay, &nYear);
	std::string month_name("JanFebMarAprMayJunJulAugSepOctNovDec");
	std::string::size_type pos = month_name.find(buff);
	nMonth = pos / 3 + 1;

	//编译发行版本时，改SVN_VERSION为SVN代码版本号
	snprintf(buff, sizeof(buff), "Build %4d%.2d%.2d %s %dv", nYear, nMonth, nDay, configType, 1);
	return buff;
}


int main() {
	LibLog_SetLogExpireTime(15);//日志保存15天
	LibLog_SetDir("log");
	LibLog_SetFlushLines(50);
	LogInfo((GetCompileVersion().c_str()));
	LogInfo(("采集服务正在启动..."));
	gateway_taskpool::get_instance()->start();
	CPagefile::GetInstance().Start();
	gateway_acceptor_observer* observer = new gateway_acceptor_observer();
	gateway_acceptor* acceptor = new gateway_acceptor();
	acceptor->set_event_handle(observer);

	boost::property_tree::ptree reader;
	//std::string server_ip = "";
	unsigned short cdma_port = 0;
	boost::property_tree::ini_parser::read_ini("collectionsvr.ini", reader);
	//server_ip = reader.get<std::string>("collection.SERVER_IP");
	cdma_port = reader.get<unsigned short>("collection.CDMA_PORT");
	bool result = acceptor->start("0.0.0.0", cdma_port);
	if (result == false)
	{
		LogError(("采集服务启动失败..."));
		return 0;
	}
	LogInfo(("采集服务已启动..."));
	run();
	acceptor->stop();
	gateway_cache::get_instance()->clear();
	gateway_taskpool::get_instance()->stop();
	//delete acceptor;
	//delete observer;
	sleep(1);
	LogInfo(("采集服务已停止..."));
	return 0;
}